package com.xywy.wjk.widget;

import java.util.Timer;
import java.util.TimerTask;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import com.xywy.wjk.WjkFragment;

/**
 * @ClassName: ConfigurationView
 * @Description:血压计测量展示 
 * @author ZhiluCui
 * @date 2014年10月20日 上午11:19:21
 * @version:1.0
 */
public class ConfigurationView extends View {
	// 柱状图画笔
	private Paint paint;
	// 字体画笔
	private Paint font_Paint;
	private Paint line_paint;
	// 数值显示的偏移量
	private int numWidth = 9;

	// 起始高度为 最大高度减去 80 【注意这里的高度是反的，也就是说，y轴是逐渐减少的】
	private float startHeight = WjkFragment.HEIGHT + 300;
//	private float endHeight = startHeight;
	private float endHeight = 0;
	public float getEndHeight() {
		return endHeight;
	}

	public void setEndHeight(float endHeight) {
		this.endHeight = (float)(endHeight*1.5);
		invalidate();
	}

	// 柱状图的宽度
	private int viewWidth = 250;

	// 组态图的高度 【显示的数值，100 为 100%】
	private int maxSize = 200;
	private int indexSize = 0;
	// 要显示的模式 类型，比如：℃和百分比等
	private String displayMode = "%";
	// 模式
	private boolean mode = false;
	public int getMaxSize() {
		return maxSize;
	}

	public void setMaxSize(int maxSize) {
		this.maxSize = maxSize;
	}

	private float startX = 100;
	// 线程控制
	private boolean display = true;
	// 是否开启动画效果
	private boolean animMode = true;
	
	private boolean flag = false; //弄个假的在哪上升
	
	private Timer timer;
	
	private int displayHeight = 0;

	/**
	 * 
	 * @param context
	 * @param maxSize
	 *            需要显示的数值
	 * @param displayMode
	 *            显示的类型
	 */
	public ConfigurationView(Context context, int maxSize, String displayMode) {
		super(context);
		this.maxSize = maxSize;
		this.displayMode = displayMode;
		init();
	}

	/**
	 * @param context
	 * @param maxSize
	 *            需要显示的数值
	 * @param displayMode
	 *            显示的类型
	 * @param mode
	 *            显示的模式，默认为false，数值越高，颜色越偏向红色。为true反之
	 */
	public ConfigurationView(Context context, int maxSize, String displayMode,
			boolean mode, float startX) {
		super(context);
		this.maxSize = maxSize;
		this.displayMode = displayMode;
		this.mode = mode;
		this.startX = startX;
		init();
	}

	/**
	 * @param context
	 * @param maxSize
	 *            需要显示的数值
	 * @param displayMode
	 *            显示的类型
	 * @param mode
	 *            显示的模式，默认为false，数值越高，颜色越偏向红色。为true反之
	 * @param animMode
	 *            是否显示动画加载效果，默认为true
	 */
	public ConfigurationView(Context context, int maxSize, String displayMode,
			boolean mode, boolean animMode, float startX) {
		super(context);
		this.maxSize = maxSize;
		this.displayMode = displayMode;
		this.mode = mode;
		this.animMode = animMode;
		this.startX = startX;
		init();
	}

	// 绘制界面
	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		paint.setARGB(255, 243, 229, 154);
		startHeight = getMeasuredHeight();
		canvas.drawRect(10, startHeight-displayHeight, 10 + viewWidth, startHeight, paint);//
//		if (!display) {
//			canvas.drawRect(10, endHeight, 10 + viewWidth, startHeight, paint);
//			paint.setARGB(255, 243, 229, 154);// 黄色
//
//			// 有动画
//			canvas.drawText("" + indexSize, numWidth, endHeight - 5, paint);
//			paint.setARGB(255, 243, 229, 154);// 黄色
//		}
		canvas.drawText(displayMode, 10, startHeight-displayHeight-80, font_Paint);
		canvas.drawText(displayHeight+"", 10, startHeight-displayHeight-40, font_Paint);
	}

	// 初始化
	private void init() {
		// 数值初始化
		// 矩形
		paint = new Paint();
		paint.setARGB(255, 243, 229, 154);// 黃色
		paint.setTextSize(100);

		// 下划线
		line_paint = new Paint();
		line_paint.setARGB(255, 223, 223, 223);
		line_paint.setStrokeWidth(30);
		// 写字笔
		font_Paint = new Paint();
		font_Paint.setARGB(255, 223, 223, 223);// 灰色
		font_Paint.setTextSize(50);

		// 设置数值显示的偏移量，数值越小，偏移量越大
		numWidth = 100;
		if (maxSize < 10) {
			numWidth = 15;
		} else if (maxSize < 100) {
			numWidth = 12;
		} else if (maxSize > 100) {
			numWidth = 20;
		}

		if (animMode) {
			// 启动一个线程来实现柱状图缓慢增高
//			thread.start();
		} else {
			display = false;
			indexSize = maxSize;
			endHeight = startHeight - (float) (maxSize * 5);
			invalidate();
		}
	}

	private Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			if (msg.what == 1 && indexSize < maxSize && endHeight >= 20) {
				endHeight -= 1.5;
				indexSize += 1;
			} else {
				display = false;
			}
			invalidate();
		}
	};

//	private Thread thread = new Thread() {
//		@Override
//		public void run() {
//			while (!Thread.currentThread().isInterrupted() && display) {
//				Message msg = new Message();
//				msg.what = 1;
//				handler.sendMessage(msg);
//				try {
//					Thread.sleep(15);
//				} catch (InterruptedException e) {
//					System.err.println("InterruptedException！线程关闭");
//					this.interrupt();
//				}
//			}
//		}
//	};

	// 刷新View
	public void toInvalidate() {
		this.invalidate();
	}
	
	public void openProcess(){
		 if(!flag){
			 flag = true;
			  timer = new Timer();
			 timer.schedule(new TimerTask() {
				
				@Override
				public void run() {
					// TODO Auto-generated method stub
//					if(endHeight == 0 && displayHeight <maxSize){
//						displayHeight++;
//						timerHandler.sendEmptyMessage(0);
//					}else {
//						if(endHeight>0 && endHeight <=maxSize){
//							 if(endHeight>displayHeight){
//								 displayHeight++;
//								 timerHandler.sendEmptyMessage(0);
//							 }else if (endHeight<displayHeight) {
//								 displayHeight--;
//								 timerHandler.sendEmptyMessage(0);
//							}else {
//								timer.cancel();
//							}
//						}else {
//							
//						}
//					}
					
					if(endHeight ==0){//
						displayHeight++;
						timerHandler.sendEmptyMessage(0);
					}else if (endHeight>0) {
						if(endHeight <maxSize){
							if(endHeight>displayHeight){
								displayHeight++;
								timerHandler.sendEmptyMessage(0);
							}else if (endHeight==displayHeight) {
								timer.cancel();
							}else {
								displayHeight--;
								timerHandler.sendEmptyMessage(0);
							}
						}else if(endHeight ==maxSize) {
							if(endHeight>displayHeight){
								displayHeight++;
								timerHandler.sendEmptyMessage(0);
							}else if (endHeight==displayHeight) {
								timer.cancel();
							}else {
								displayHeight--;
								timerHandler.sendEmptyMessage(0);
							}
						}else {
							
						}
					}else {
						
					}
				}
			}, 0,15);
		 }
	}
	
	private Handler timerHandler = new Handler(){
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 0:
				invalidate();
				break;

			default:
				break;
			}
		};
	};
}
